package com.lc.common.bean.response;

import com.lc.common.bean.enums.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 全局返回对象列表封装
 * @author lc
 * @param <T>
 */
@AllArgsConstructor
@ApiModel(value = "Result", description = "统一返回类")
public class Result<T> implements Serializable{

    private static final long serialVersionUID = -3357827064456876976L;

    /**
     * 服务内部错误编码
     */
    private static final String ERROR_CODE = ResponseCode.ERROR_CODE.getCode();
    /**
     * 请求成功
     **/
    private static final String SUCCESS_CODE = ResponseCode.SUCCESS_CODE.getCode();
    /**
     * 参数错误编码
     */
    private static final String ERROR_CODE_PARAM = ResponseCode.ERROR_CODE_PARAM.getCode();

    /**
     * 成功信息
     */
    private static final String SUCCESS_MSG = "操作成功！";

    /**
     * 成功信息
     */
    private static final String ERROR_MSG = "操作失败！";

    @ApiModelProperty(value = "请求结果是否成功")
    @Getter
    private boolean success = false;

    @ApiModelProperty(value = "请求编码")
    @Getter
    private String code;

    @ApiModelProperty(value = "请求备注信息")
    @Setter
    @Getter
    private String msg;

    /**
     * 不可修改
     */
    @ApiModelProperty(value = "请求结果对象")
    @Getter
    private final T obj;

    /**
     * 总条数
     */
    @Getter
    private long total = 0L;

    /**
     * 总页数
     */
    @Getter
    private long pages = 0L;

    public Result(boolean success, String code, String msg, T obj) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.obj = obj;
    }

    private Result(){
        this.obj = null;
    }

    private Result(T result){
        this.obj = result;
    }

    public static <T> Result<T> success(T result,String msg) {
        Result<T> item = success(result);
        item.msg = msg;
        return item;
    }
    public static <T> Result<T> success(T result) {
        Result<T> item = new Result<>(result);
        item.code = SUCCESS_CODE;
        item.success = true;
        item.msg = SUCCESS_MSG;
        return item;
    }
    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> successPage(T result,long total,long pages,String msg) {
        Result<T> item = successPage(result,total,pages);
        item.msg = msg;
        return item;
    }

    public static <T> Result<T> successPage(T result,long total,long pages) {
        Result<T> item = new Result<>(result);
        item.code = SUCCESS_CODE;
        item.success = true;
        item.msg = SUCCESS_MSG;
        item.total = total;
        item.pages = pages;
        return item;
    }

    public static <T> Result<T> failure() {
        return failure(ERROR_MSG);
    }

    public static <T> Result<T> failure(String errorMessage) {
        Result<T> item = new Result<>();
        item.code = ERROR_CODE;
        item.success = false;
        item.msg = errorMessage;
        return item;
    }

    public static <T> Result<T> paramFailure(String errorMessage) {
        Result<T> item = new Result<>();
        item.success = false;
        item.code = ERROR_CODE_PARAM;
        item.msg = errorMessage;
        return item;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", obj=" + obj +
                ", total=" + total +
                ", pages=" + pages +
                '}';
    }
}
